package proxyPool

import (
	"ahutoj/web/service/originJudge/proxyPool/fetcher"
	"ahutoj/web/service/originJudge/proxyPool/ip"
	"ahutoj/web/utils"
	"context"
	"sync"
	"time"
)

func Task(ctx context.Context) {
	logger := utils.GetLogInstance()
	ipChan := make(chan *ip.IP, 2000)

	// Check the IPs in DB
	go func() {
		for{
			CheckProxyDB(ctx)
		}
	}()

	// Check the IPs in channel
	for i := 0; i < 20; i++ {
		go func() {
			for {
				CheckProxy(ctx, <-ipChan)
				time.Sleep(6 * time.Second)
			}
		}()
	}

	// Start getters to scraper IP and put it in channel
	for {
		nums := ip.CountIp(ctx)
		logger.Printf("Chan: %v, IP: %d\n", len(ipChan), nums)
		if len(ipChan) < 100 {
			go run(ipChan)
		}
		time.Sleep(6000 * time.Second)
	}
}

func run(ipChan chan<- *ip.IP) {
	logger := utils.GetLogInstance()
	var wg sync.WaitGroup
	siteFuncList := []func() []*ip.IP{
		fetcher.Ip33661,
		fetcher.Ip33662,
		fetcher.IpDoc,
	}
	for _, siteFunc := range siteFuncList {
		wg.Add(1)
		Func:=siteFunc
		go func() {
			// temp := Func()
			for _, v := range Func() {
				ipChan <- v
			}
			wg.Done()
		}()
	}
	wg.Wait()
	logger.Println("All getters finished.")
}
